<?php
/* CAT:Bar Chart */

/* pChart library inclusions */
include_once("../class/pData.class.php");
include_once("../class/pDraw.class.php");
include_once("../class/pImage.class.php");

include_once("graph-settings.php");

function drawMyBarGraph($series, $valuesHeader, $itemNames, $itemHeader,
 $title, $optionalParams = NULL) {

	$drawLabelsBottom = isset($optionalParams["drawLabelsBottom"]) ? $optionalParams["drawLabelsBottom"] : FALSE;
		
	/* Create and populate the pData object */
	$MyData = new pData();
	$MyData->addPoints($series, $valuesHeader);
	$MyData->addBarLabels($itemNames, $valuesHeader);
 	$MyData->addPoints($itemNames, $itemHeader);
 	$MyData->setSerieDescription($itemHeader, $itemHeader);
 	$MyData->setAbscissa($itemHeader);

 	$myPicture = prepareGraph($MyData, $title, $itemHeader, $valuesHeader, FALSE);
 	
	$myPicture->drawScale(array("DrawSubTicks"=>TRUE, "AllLabelsSkip"=>!($drawLabelsBottom),
		"Mode" => SCALE_MODE_START0));
	
	$myPicture->setShadow(TRUE, array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));
	$myPicture->drawBarChart(array("DisplayValues" => !$drawLabelsBottom, "DisplayColor"=>DISPLAY_AUTO,"Rounded"=>TRUE,"Surrounding"=>30));
	$myPicture->setShadow(FALSE);

	/* Render the picture (choose the best way) */
	$myPicture->autoOutput("bargraph-$itemHeader.png");
}

function drawMyStackedBarGraph($series, $valuesHeader, $itemNames, $itemHeader,
	$title, $colors) {

	/* Create and populate the pData object */
	$MyData = new pData();
	
	$i = 0;
	foreach ($series as $serieName => $serie) {
		$color = $colors[$i];
		$i = ($i + 1) % count($colors);
		
		$MyData->addPoints($serie, $serieName);
		$MyData->setPalette($serieName, $color);
	}
	
	$MyData->addPoints($itemNames, $itemHeader);
	$MyData->setAbscissa($itemHeader);
	
	$myPicture = prepareGraph($MyData, $title, $itemHeader, $valuesHeader, TRUE);
	
	$myPicture->drawScale(array("DrawSubTicks"=>TRUE, "Mode"=>SCALE_MODE_ADDALL_START0));

	$myPicture->setShadow(TRUE, array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));
	$myPicture->setFontProperties(array("FontName"=>"../../fonts/pf_arma_five.ttf", "FontSize"=>12));

	$myPicture->drawStackedBarChart(array("Rounded"=>TRUE,"DisplayValues"=>TRUE,"DisplayColor"=>DISPLAY_AUTO,"DisplaySize"=>6,"BorderR"=>255,"BorderG"=>255,"BorderB"=>255));
 	
 	$myPicture->setShadow(FALSE);

	/* Render the picture (choose the best way) */
	$myPicture->autoOutput("bargraph-$itemHeader.png");
}

function prepareGraph($graphData, $title, $xAxisName, $yAxisName, $useLegend) {
	$graphWidth = GRAPH_WIDTH;
	$graphHeight = GRAPH_HEIGHT;
	
	$titleVerticalSpace = 60;
	$legendVerticalSpace = $useLegend ? 30 : 0;
	$labelsVerticalSpace = 20;
	$xAxisNameSpace = 30;
	
	$leftMargin = 10;
	$yAxisNameSpace = 20; // should be determined by getTextBox
	$spaceBetweenYAxisAndName = 30;
	$leftHorizontalSpace = $leftMargin + $yAxisNameSpace + $spaceBetweenYAxisAndName;
	$rightHorizontalSpace = 35;
	
	$width = $graphWidth - $leftHorizontalSpace - $rightHorizontalSpace;
	$height = $graphHeight - $titleVerticalSpace - $legendVerticalSpace - $labelsVerticalSpace - $xAxisNameSpace;
	
	$x1 = $leftHorizontalSpace;
	$y1 = $titleVerticalSpace + $legendVerticalSpace;
	$x2 = $x1 + $width;
	$y2 = $y1 + $height;

	$titleFormat = array("FontName"=>"../../fonts/verdana.ttf", "FontSize"=>20, "Align"=>TEXT_ALIGN_MIDDLEMIDDLE);
	$xAxisNameFormat = array("FontName"=>"../../fonts/verdana.ttf", "FontSize"=>12, "Align"=>TEXT_ALIGN_MIDDLEMIDDLE);
	$yAxisNameFormat = array("FontName"=>"../../fonts/verdana.ttf", "FontSize"=>12, "Align"=>TEXT_ALIGN_MIDDLEMIDDLE, "Angle" => 90);
	$legendFormat = array("FontName"=>"../../fonts/Forgotte.ttf", "FontSize"=>14, "Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL);
	
	/* Create the pChart object */
	$myPicture = new pImage($graphWidth, $graphHeight, $graphData);
	$myPicture->setFontProperties(array("FontName"=>"../../fonts/Forgotte.ttf", "FontSize"=>14));
	
	/* Draw the background */
	$Settings = array("R"=>170, "G"=>183, "B"=>87, "Dash"=>1, "DashR"=>190, "DashG"=>203, "DashB"=>107);
	$myPicture->drawFilledRectangle(0, 0, $graphWidth, $graphHeight, $Settings);
	
	/* Overlay with a gradient */
	$Settings = array("StartR"=>219, "StartG"=>231, "StartB"=>139, "EndR"=>1, "EndG"=>138, "EndB"=>68, "Alpha"=>50);
	$myPicture->drawGradientArea(0, 0, $graphWidth, $graphHeight, DIRECTION_VERTICAL,$Settings);
	
	/* Add a border to the picture */
	$myPicture->drawRectangle(0, 0, $graphWidth - 1, $graphHeight - 1, array("R"=>0,"G"=>0,"B"=>0));
	
	$xAxisLabelMiddleX = $leftHorizontalSpace + ($width / 2);
	$xAxisLabelMiddleY = $graphHeight - ($xAxisNameSpace / 2);
	$myPicture->drawText($xAxisLabelMiddleX, $xAxisLabelMiddleY, $xAxisName, $xAxisNameFormat);
	
	/* Write the y axis label */
	$yAxisLabelMiddleX = $leftMargin + ($yAxisNameSpace / 2);
	$yAxisLabelMiddleY = ($y1 + $y2) / 2;
	$myPicture->drawText($yAxisLabelMiddleX, $yAxisLabelMiddleY, $yAxisName, $yAxisNameFormat);
	
	/* Write the chart title */
	$myPicture->drawText($graphWidth / 2, $titleVerticalSpace / 2, $title, $titleFormat);
	
	$myPicture->setGraphArea($x1, $y1, $x2, $y2);
	$myPicture->drawFilledRectangle($x1, $y1, $x2, $y2, array("R"=>255,"G"=>255,"B"=>255,"Surrounding"=>-200,"Alpha"=>10));
	
	/* Write the legend */
	if ($useLegend) {
		$myPicture->drawLegend($leftHorizontalSpace, $titleVerticalSpace, $legendFormat);
	}
		
	return $myPicture;
}
?>
